﻿/*
 * MapyCzFeatureSet
 * 
 * initial implementation : 18.7.2011
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections;
using System.Collections.Generic;
using Petra.Gis.DataServices;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;

namespace Petra.Gis.DataServices.MapyCz
{
  public abstract partial class MapyCzFeatureSet : FeatureSetBase
  {

    private FeatureSpatialFilter _lastFilter;

    /// <summary>
    /// xaml constr.
    /// </summary>
    protected MapyCzFeatureSet()
      :base()
    {
      Metadata = pripravMetadata(MapType.baseMap, "základní mapa celé Evropy, datový zdroj:Navtech");
      _data = new Dictionary<string, Grid>();
    }



    protected abstract string kodMapy { get; set; }



    private Dictionary<string, Grid> _data;

    protected override IEnumerable data 
    {
      get
      {
        //if parameter for spatial filter changed
        //update _data...
        if (Parameters.ContainsKey("Petra.Gis.DataServices.FeatureSpatialFilter"))
          prepareSpatialFilter();

        FeatureSpatialFilter sf = (FeatureSpatialFilter)Parameters["Petra.Gis.DataServices.FeatureSpatialFilter"].Value;

        if (_lastFilter.Bounds == null || _lastFilter.GridResolution == 0)
          obnovDlazdiceProMapovyVyrez();

        else if (_lastFilter.Bounds != sf.Bounds && _lastFilter.GridResolution == sf.GridResolution)
          ObnovMapuPriPosuvu(sf.Bounds);

        else if (_lastFilter.GridResolution != sf.GridResolution)
          obnovDlazdiceProMapovyVyrez();

        //update last filter...
        _lastFilter.Bounds = sf.Bounds.Clone();
        _lastFilter.GridResolution = sf.GridResolution;

        return _data; 
      }
      set { _data = (Dictionary<string, Grid>)value; }
    }




    new public Dictionary<string, Grid> Data
    {
      get { return (Dictionary<string, Grid>)data; }
      set { data = value; }
    }




    /// <summary>
    /// unsupported for Mapy.cz
    /// </summary>
    protected override IGeometryConverter geometryConverter
    {
      get { return null; }
      set { }
    }




    /// <summary>
    /// if spatial filter is missing
    /// </summary>
    private void prepareSpatialFilter()
    {
      FeatureSpatialFilter f = new FeatureSpatialFilter()
      {
        Bounds = BoundingBox.Parse("3641212;1981739;5922736;8302847"),
        GridResolution = 1000.0 / 4700000.0 //for top level map
      };
      Parameters.Add("Petra.Gis.DataServices.FeatureSpatialFilter"
        , new FeatureSetParameter()
        {
          ParameterName = "Petra.Gis.DataServices.FeatureSpatialFilter"
        ,
          Value = f
        });
    }


  }
}
